package com.example.sys.ai.dto;

import com.alibaba.fastjson.JSONObject;
import com.example.core.utils.StringUtils;
import lombok.Data;

import java.io.Serializable;

@Data
public class BankCard implements Serializable {

    public static final String WORDS_RESULT = "words_result";

    public static final String NAME = "bank_name";

    public static final String CODE = "bank_card_number";

    public static final String VALID = "valid_date";

    public static final String WORDS = "words";

    public static final String RESULT = "result";

    //银行名称
    private String name;

    //卡号
    private String code;

    //签发日期
    private String valid;

    /***
     * ocr识别银行卡
     * @param json
     * @return
     */
    public static BankCard toCard(String json){
        BankCard bankCard = new BankCard();
        if (StringUtils.isNotEmpty(json)){
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject.containsKey(RESULT)){
                JSONObject resultJson = jsonObject.getJSONObject(RESULT);
                //银行
                if (resultJson.containsKey(NAME) && StringUtils.isNotEmpty(resultJson.getString(NAME))){
                    bankCard.setName(resultJson.getString(NAME));
                }
                //卡号
                if (resultJson.containsKey(CODE) && StringUtils.isNotEmpty(resultJson.getString(CODE))){
                    bankCard.setCode(resultJson.getString(CODE));
                }
                //签发日期
                if (resultJson.containsKey(VALID) && StringUtils.isNotEmpty(resultJson.getString(VALID))){
                    bankCard.setValid(resultJson.getString(VALID));
                }
            }
        }
        return bankCard;
    }
}
